package com.powernode.config;

import com.powernode.filter.TokenTranslateFilter;
import com.powernode.filter.password.AccountLoginFilter;
import com.powernode.filter.password.AccountProvider;
import com.powernode.filter.sms.SmsLoginFilter;
import com.powernode.filter.sms.SmsProvider;
import com.powernode.handler.LoginFailHandler;
import com.powernode.handler.LoginSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author: powernode
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LoginSuccessHandler loginSuccessHandler;

    @Autowired
    private LoginFailHandler loginFailHandler;

    @Autowired
    private AccountProvider accountProvider;

    @Autowired
    private SmsProvider smsProvider;

    @Autowired
    private AuthenticationManager authenticationManager;


    @Autowired
    private TokenTranslateFilter tokenTranslateFilter;

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(accountProvider);
        auth.authenticationProvider(smsProvider);
    }

    /**
     * 创建密码登陆过滤器
     *
     * @return
     */
    public AccountLoginFilter accountLoginFilter() {
        return new AccountLoginFilter(authenticationManager, loginFailHandler, loginSuccessHandler);
    }

    /**
     * 创建短信登陆过滤器
     *
     * @return
     */
    public SmsLoginFilter smsLoginFilter() {
        return new SmsLoginFilter(authenticationManager, loginFailHandler, loginSuccessHandler);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.cors().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // 创建自己的过滤器 放在认证之前
        http.addFilterBefore(accountLoginFilter(), UsernamePasswordAuthenticationFilter.class);
        http.addFilterBefore(smsLoginFilter(), UsernamePasswordAuthenticationFilter.class);

        http.addFilterBefore(tokenTranslateFilter, UsernamePasswordAuthenticationFilter.class);
        http.authorizeRequests().anyRequest().authenticated();
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
